<html>
<head>
    <title>修改密码</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
    <script src="/layui/jquery.min.js"></script>
</head>
<body>
<div class="main" style="width: 90%;margin: 15px auto">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label" for="username">用户名</label>
            <div class="layui-input-block">
                <input type="text" readonly id="username" name="username" value="" class="layui-input"
                       placeholder="请输入用户名">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" for="oldPwd">原密码</label>
            <div class="layui-input-block">
                <input type="password" id="oldPwd" name="oldPwd" lay-verify="required" class="layui-input"
                       placeholder="请输入原密码">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" for="password">新密码</label>
            <div class="layui-input-block">
                <input type="password" id="password" name="password" lay-verify="required" class="layui-input"
                       placeholder="请输入新密码">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" for="confirmPwd">确认密码</label>
            <div class="layui-input-block">
                <input type="password" id="confirmPwd" name="confirmPwd" lay-verify="required|confirm"
                       class="layui-input" placeholder="请再次输入密码">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-filter="submit" lay-submit>提交</button>
            </div>
        </div>
    </form>
</div>
</body>
<script src="/util/cookies.js"></script>
<script>
    $("#username").val(loginInfo.username)
    layui.use(['form'], function () {
        const form = layui.form;
        form.verify({
            confirm: function (value) {
                if (value !== $("#password").val()) {
                    return "两次密码不一致"
                }
            }
        });
        form.on("submit(submit)", function (data) {
            data.field.id = loginInfo.id
            $.ajax({
                url: "/userinfo/password",
                method: "post",
                data: data.field,
                dataType: "json",
                success(res) {
                    if (res.code === 200) {
                        layer.msg('操作成功！请重新登录', {icon: 6}, function () {
                            top.location.replace("/logout")
                        })
                    } else {
                        layer.alert(res.msg, {icon: 5});
                    }
                }
            });
            return false;
        })
    })
</script>
</html>
